package com.web_bloks.widget.client;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.Element;

import com.google.gwt.user.client.ui.Panel;
import com.web_bloks.system.client.Logger;
//import com.google.gwt.core.client.GWT;

import com.web_bloks.widget.client.WidgetEx;

/**
 * WidgetFactory
 * 
 * @author Jean-Lou Dupont
 *
 */
public class WidgetFactory {

	final static String thisClass = "com.web_bloks.widget.WidgetFactory";
	
	static String[] liste = new String[] {
		"rating", "formwidget", "smswidget"
	};

	/**
	 * Main method for this class
	 * 
	 * @param collectionName
	 * @param panel
	 * @param node
	 */
	public static void createAllFromNode(String collectionName, Panel panel,Element node) {
		Logger.logInfo(thisClass+".createAllFromNode, collectionName:" + collectionName);
		
		Document doc = Document.get();
		
		NodeList<Element> lForm = doc.getElementsByTagName("form");
		NodeList<Element> lDiv  = doc.getElementsByTagName("div");
		NodeList<Element> lTable  = doc.getElementsByTagName("table");
		
		processList( collectionName, panel, lForm, "form" );
		processList( collectionName, panel, lDiv, "div" );
		processList( collectionName, panel, lTable, "table" );
		
		// does this really help the GC?
		lForm = null;
		lDiv  = null;
		lTable = null;
	}
		
	
	/**
	 * Creates a 'Widget' object instance based
	 * on simple string 
	 * 
	 * @param className
	 * @return widget element
	 */
	@SuppressWarnings("all")
	public static WidgetEx create(Element parent, Element source, String className) {
		Logger.logInfo(thisClass+".create, className:" + className);
		
		if ( null == className ) 
			throw new RuntimeException(thisClass+".isDefined: className can't be null");

		if ( null == source ) 
			throw new RuntimeException(thisClass+".isDefined: source can't be null");

		WidgetEx w = null;
		
		if ( "rating" == className ) 
			w = (WidgetEx) new com.web_bloks.widget.client.Rating( parent, source );

		if ( "formwidget" == className ) 
			w = (WidgetEx) new com.web_bloks.widget.client.FormWidget(parent, source);
		
		if ( "smswidget" == className ) 
			w = (WidgetEx) new com.web_bloks.widget.client.SMSWidget(parent, source);

		return w;
	}
	
	public static int isDefined(String className){
		//Logger.logInfo(thisClass+".isDefined, className:" + className);
		
		if ( null == className ) 
			throw new RuntimeException(thisClass+".isDefined: className can't be null");
		
		for (int i=0;i<liste.length;i++)
			if ( className == liste[i] )
				return i;
		
		return -1;
	}
	
	protected static void processList(String collectionName, Panel panel, NodeList<Element> lst, String className) {
		
		if ( null == lst )
			return;
		
		Logger.logInfo(thisClass+".processList, className:"+className+",lst.getLength(): " + lst.getLength() );		
		
		String cName, id = null;
		Element es, parent;
		int index = -1;
		WidgetEx w;
		
		for(int i=0;i<lst.getLength(); i++) {
			es = lst.getItem(i);
			
			cName = es.getAttribute("class");
			Logger.logInfo(thisClass+".processList, cName:"+cName);	
			
			if ( null == cName )
				continue;
			
			index = isDefined( cName );
			
			if ( -1 != index ) {

				parent = es.getParentElement();
				
				w = create( parent, es, cName );
				w.setOriginCollection(collectionName);

				id = w.getId();
				
				// add to collection so we can track it
				if ( null != id ) {
					WidgetManager wm = WidgetManager.create();
					wm.add(collectionName, id, w);
				}
				else
					Logger.logWarn(thisClass+".processList: widget missing an id");
			}
				
		}//FOR
		
	}//END
	
	
	public static void clearCollection(String collectionName) {
		WidgetManager wm = WidgetManager.create();
		wm.clear( collectionName );
	}
	
}//ENDCLASS
